package com.shms.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SQLQuery;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.shms.bean.GroupOfGrade;
import com.shms.dao.ShGradeDao;
import com.shms.entity.ShClass;
import com.shms.entity.ShGrade;
import com.shms.until.PageUntil;

/**
 * 文件名：ShGradeDaoImpl.java
 * 编写者：
 * 创建时间：2013-1-3
 * 简要描述：
 *
 */
public class ShGradeDaoImpl extends HibernateDaoSupport implements ShGradeDao {

	@SuppressWarnings("finally")
	@Override
	public GroupOfGrade queryGrade(GroupOfGrade groupOfGrade) {
		GroupOfGrade gOfGradeRtn = new GroupOfGrade();
		try {
			//查询数据总条数的SQL语句
			String sql = "SELECT COUNT(*) FROM SH_GRADE";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			//获取总条数
			int allRow = Integer.parseInt(String.valueOf(sqlQuery.uniqueResult()));
			//获取总页数
			int totalPage = PageUntil.countTotalPage(groupOfGrade.getPageSize(), allRow);
			//获取要查询显示的数据的页数
			int currentPage = PageUntil.countCurrentPage(groupOfGrade.getCurrentPage(), totalPage);
			//获取要查询显示的数据的第一条的数字
			int currentRow = PageUntil.countCurrentRow(groupOfGrade.getPageSize(), currentPage);
			
			sql = "SELECT * FROM SH_GRADE";
			sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			//设置查询的第一条数据位置
			sqlQuery.setFirstResult(currentRow);
			//设置查询最大条数
			sqlQuery.setMaxResults(groupOfGrade.getPageSize());
			@SuppressWarnings("unchecked")
			List<ShGrade> trendsListRtn = sqlQuery.addEntity(ShGrade.class).list();
			gOfGradeRtn = new GroupOfGrade(currentPage, totalPage, allRow, trendsListRtn); 
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return gOfGradeRtn;
		}
	}

	@SuppressWarnings("finally")
	@Override
	public boolean batchDelete(Integer[] ids) {
		boolean rtn = true;
		try {
			String sql = "DELETE FROM SH_GRADE T WHERE T.GID NOT IN (SELECT T.GID FROM SH_GRADE T,SH_CLASS C WHERE C.GID=T.GID ) AND T.GID IN (:ids)";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setParameterList("ids", ids);
			sqlQuery.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}

	@SuppressWarnings("finally")
	@Override
	public boolean saveGrades(ShGrade shGrade) {
		boolean rtn = true;
		try {
			this.getSessionFactory().getCurrentSession().save(shGrade);
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}

	@SuppressWarnings("finally")
	@Override
	public boolean updateGrades(ShGrade shGrade) {
		boolean rtn = true;
		try {
			this.getSessionFactory().getCurrentSession().update(shGrade);
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}

	@SuppressWarnings("finally")
	@Override
	public ShGrade queryById(ShGrade shGrade) {
		ShGrade shGradeRtn = new ShGrade();
		try {
			String sql = "SELECT * FROM SH_GRADE WHERE GID = :gid";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setProperties(shGrade);
			@SuppressWarnings("unchecked")
			List<ShGrade> listGrade = sqlQuery.addEntity(ShGrade.class).list();
			if (listGrade.size() == 1) {
				shGradeRtn = listGrade.get(0);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return shGradeRtn;
		}
	}
	
	@SuppressWarnings({ "unchecked", "finally" })
	public List<ShGrade> queryAllGname(){
		List<ShGrade> shg = new ArrayList<ShGrade>();
		try {
			String sql = "SELECT * FROM SH_GRADE";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			shg = sqlQuery.addEntity(ShGrade.class).list();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return shg;
		}
	}

	@SuppressWarnings({ "unchecked", "finally" })
	@Override
	public List<ShClass> queryClassesById(ShGrade shGrade) {
		List<ShClass> shc = new ArrayList<ShClass>();
		try {
			String sql = "SELECT C.* FROM SH_CLASS C,SH_GRADE G WHERE G.GID= C.GID AND G.GID= :gid";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setProperties(shGrade);
			shc = sqlQuery.addEntity(ShClass.class).list();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return shc;
		}
	}
}
